home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / SavedSearch.cs500 < prev    next >
Text File  |  2007-12-01  |  41KB  |  998 lines

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text;
  5. using System.Web.UI.WebControls;
  6. using System.Xml;
  7. using GBPVR.Public;
  8. using GBPVRSchedule;
  9. using Microsoft.Win32;
  10.  
  11. /// <summary>
  12. /// Summary description for SavedSearch
  13. /// </summary>
  14. /// 
  15. namespace gbweb
  16. {
  17.     #region Object used to save and load searches
  18.     public class SavedSearch
  19.     {
  20.         private string searchName;
  21.         private bool autoShowSearch;
  22.         private bool autoRecordSearch;
  23.         private bool searchTitle;
  24.         private bool searchSubTitle;
  25.         private bool searchDescription;
  26.         private string searchPhrase;
  27.         private bool searchCaseSensitive;
  28.         private ArrayList searchGenres;
  29.         private string searchLengthMin;
  30.         private string searchLengthMax;
  31.         private string searchTreeviewSortOrder;
  32.         private ArrayList searchChannels;
  33.         private string searchStartDate;
  34.         private string searchStartTime;
  35.         private string searchEndDate;
  36.         private string searchEndTime;
  37.         private string searchVersion;
  38.  
  39.         public SavedSearch()
  40.         {
  41.          ///Object used to manipulate saved searches
  42.             searchName = string.Empty;
  43.             autoShowSearch = false;
  44.             autoRecordSearch = false;
  45.             searchTitle = true;
  46.             searchSubTitle = false;
  47.             searchDescription = false;
  48.             searchPhrase = string.Empty;
  49.             searchCaseSensitive = false;
  50.             searchGenres = new ArrayList();
  51.             searchLengthMin = string.Empty;
  52.             searchLengthMax = string.Empty;
  53.             searchTreeviewSortOrder = string.Empty;
  54.             searchChannels = new ArrayList();
  55.             searchStartDate = null;
  56.             searchStartTime = null;
  57.             searchEndDate = null;
  58.             searchEndTime = null;
  59.             searchVersion = null;
  60.         }
  61.  
  62.         public SavedSearch(string searchName, bool autoShowSearch, bool autoRecordSearch, bool searchTitle, bool searchSubTitle, bool searchDescription, string searchPhrase, bool searchCaseSensitive,
  63.                            ArrayList searchGenres, string searchLengthMin, string searchLengthMax, string searchTreeviewSortOrder, ArrayList searchChannels, string searchStartDate, 
  64.                            string searchStartTime, string searchEndDate, string searchEndTime)
  65.         {
  66.             ///Object used to manipulate saved searches
  67.             this.searchName = searchName;
  68.             this.autoShowSearch = autoShowSearch;
  69.             this.autoRecordSearch = autoRecordSearch;
  70.             this.searchTitle = searchTitle;
  71.             this.searchChannels = searchChannels;
  72.             this.searchTreeviewSortOrder = searchTreeviewSortOrder;
  73.             this.searchLengthMax = searchLengthMax;
  74.             this.searchLengthMin = searchLengthMin;
  75.             this.searchGenres = searchGenres;
  76.             this.searchPhrase = searchPhrase;
  77.             this.searchCaseSensitive = searchCaseSensitive;
  78.             this.searchDescription = searchDescription;
  79.             this.searchSubTitle = searchSubTitle;
  80.             this.searchStartDate = searchStartDate;
  81.             this.searchStartTime = searchStartTime;
  82.             this.searchEndDate = searchEndDate;
  83.             this.searchEndTime = searchEndTime;
  84.             searchVersion = "2.0";
  85.         }
  86.  
  87.         public string getSearchVersion()
  88.         {
  89.             return searchVersion;
  90.         }
  91.  
  92.         public void setSearchVersion(string a)
  93.         {
  94.             searchVersion = a;
  95.         }
  96.  
  97.         public string getSearchName()
  98.         {
  99.             return searchName;
  100.         }
  101.  
  102.         public void setSearchName(string a)
  103.         {
  104.             searchName = a;
  105.         }
  106.  
  107.         public bool isAutoShowSearch()
  108.         {
  109.             return autoShowSearch;
  110.         }
  111.  
  112.         public void setautoShowSearch(bool a)
  113.         {
  114.             autoShowSearch = a;
  115.         }
  116.  
  117.         public bool isAutoRecordSearch()
  118.         {
  119.             return autoRecordSearch;
  120.         }
  121.  
  122.         public void setautoRecordSearch(bool a)
  123.         {
  124.             autoRecordSearch = a;
  125.         }
  126.  
  127.         public bool getSearchTitle()
  128.         {
  129.             return searchTitle;
  130.         }
  131.  
  132.         public void setSearchTitle(bool a)
  133.         {
  134.             searchTitle = a;
  135.         }
  136.  
  137.         public bool getSearchSubTitle()
  138.         {
  139.             return searchSubTitle;
  140.         }
  141.  
  142.         public void setSearchSubTitle(bool a)
  143.         {
  144.             searchSubTitle = a;
  145.         }
  146.  
  147.         public bool getSearchDescription()
  148.         {
  149.             return searchDescription;
  150.         }
  151.  
  152.         public void setSearchDescription(bool a)
  153.         {
  154.             searchDescription = a;
  155.         }
  156.  
  157.         public string getTreeViewSortOrder()
  158.         {
  159.             return searchTreeviewSortOrder;
  160.         }
  161.  
  162.         public void setTreeviewSortOrder(string a)
  163.         {
  164.             searchTreeviewSortOrder = a;
  165.         }
  166.  
  167.         public string getSearchLengthMin()
  168.         {
  169.             return searchLengthMin;
  170.         }
  171.  
  172.         public void setSearchLengthMin(string a)
  173.         {
  174.             searchLengthMin = a;
  175.         }
  176.  
  177.         public string getSearchLengthMax()
  178.         {
  179.             return searchLengthMax;
  180.         }
  181.  
  182.         public void setSearchLengthMax(string a)
  183.         {
  184.             searchLengthMax = a;
  185.         }
  186.  
  187.         public ArrayList getSearchGenres()
  188.         {
  189.             return searchGenres;
  190.         }
  191.  
  192.         public void setSearchGenres(ArrayList a)
  193.         {
  194.             searchGenres = a;
  195.         }
  196.  
  197.         public void addSearchGenres(string a)
  198.         {
  199.             searchGenres.Add(a);
  200.         }
  201.  
  202.         public string getSearchPhrase()
  203.         {
  204.             return searchPhrase;
  205.         }
  206.  
  207.         public void setSearchPhrase(string a)
  208.         {
  209.             searchPhrase = a;
  210.         }
  211.  
  212.         public bool isCaseSensitiveSearch()
  213.         {
  214.             return searchCaseSensitive;
  215.         }
  216.  
  217.         public void setSearchCaseSensitive(bool a)
  218.         {
  219.             searchCaseSensitive = a;
  220.         }
  221.  
  222.         public ArrayList getSearchChannels()
  223.         {
  224.             return searchChannels;
  225.         }
  226.  
  227.         public void setSearchChannels(ArrayList a)
  228.         {
  229.             searchChannels = a;
  230.         }
  231.  
  232.         public void addSearchChannels(string a)
  233.         {
  234.             searchChannels.Add(a);
  235.         }
  236.  
  237.         public void setSearchStartDate(string a)
  238.         {
  239.             searchStartDate = a;
  240.         }
  241.  
  242.         public string getSearchStartDate()
  243.         {
  244.             return searchStartDate;
  245.         }
  246.  
  247.         public void setSearchStartTime(string a)
  248.         {
  249.             searchStartTime = a;
  250.         }
  251.  
  252.         public string getSearchStartTime()
  253.         {
  254.             return searchStartTime;
  255.         }
  256.  
  257.         public void setSearchEndDate(string a)
  258.         {
  259.             searchEndDate = a;
  260.         }
  261.  
  262.         public string getSearchEndDate()
  263.         {
  264.             return searchEndDate;
  265.         }
  266.  
  267.         public void setSearchEndTime(string a)
  268.         {
  269.             searchEndTime = a;
  270.         }
  271.  
  272.         public string getSearchEndTime()
  273.         {
  274.             return searchEndTime;
  275.         }
  276.     }
  277.     #endregion
  278.  
  279.     #region Utility Class used to work with the SavedSearch Object
  280.  
  281.     public class SavedSearchUtils
  282.     {
  283.  
  284.         public DateTime toDate(string a)
  285.         {
  286.             DateTime dte = Convert.ToDateTime(a);
  287.             return dte;
  288.         }
  289.         
  290.         public DropDownList loadSearchList(DropDownList searchNames, string filter)
  291.         {
  292.             //Method to load saved search titles to a dropdown list
  293.             XmlDocument xmlDoc = new XmlDocument();
  294.             XmlNode node;
  295.             if (File.Exists(GetWebConfigFileName()))
  296.             {
  297.                 try
  298.                 {
  299.                     xmlDoc.Load(GetWebConfigFileName());
  300.                     //Load initial line of the dropdownlist
  301.                     searchNames.Items.Add("Choose Saved Search...");
  302.                     XmlNodeList searches = xmlDoc.GetElementsByTagName("SearchName");
  303.                     //Create an array to hold all the search names
  304.                     ArrayList srchNames = new ArrayList();
  305.                     //Load the found search names to the array applying the appropriate filter
  306.                     foreach (XmlNode name in searches )
  307.                     {
  308.                         if (filter != "all")
  309.                         {
  310.                             bool auto = false;
  311.                             bool auto_record = false;
  312.                             node =
  313.                                 xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + name.InnerText +
  314.                                                                         "']/SearchName");
  315.                             //Set the auto-search flag from the saved search
  316.                             try
  317.                             {
  318.                                 auto = (Convert.ToBoolean(node.Attributes["autoShowSearch"].InnerText));
  319.                             }
  320.                             catch
  321.                             {
  322.                                 auto = false;
  323.                             }
  324.  
  325.                             //Set the auto-record flag from the saved search
  326.                             try
  327.                             {
  328.                                 auto_record = (Convert.ToBoolean(node.Attributes["autoRecordSearch"].InnerText));
  329.                             }
  330.                             catch
  331.                             {
  332.                                 auto_record = false;
  333.                             }
  334.  
  335.                             if (filter == "auto" && auto)
  336.                             {
  337.                                 srchNames.Add(name.InnerText);
  338.                             }
  339.                             else if (filter == "auto-record" && auto_record)
  340.                             {
  341.                                 srchNames.Add(name.InnerText);
  342.                             }
  343.                             else if (filter == "auto-all" && (auto_record || auto))
  344.                             {
  345.                                 srchNames.Add(name.InnerText);
  346.                             }
  347.                             else if (filter == "non-auto" && !auto && !auto_record)
  348.                             {
  349.                                 srchNames.Add(name.InnerText);
  350.                             }
  351.                         }
  352.                         else
  353.                         {
  354.                             srchNames.Add(name.InnerText); 
  355.                         } 
  356.                     }
  357.                     //Sort the array of search names
  358.                     srchNames.Sort();
  359.                     //Load the sorted array of search names to the dropdownlist
  360.                     foreach (string srchname in srchNames)
  361.                     {
  362.                         searchNames.Items.Add(srchname); 
  363.                     }
  364.                 }
  365.                 catch
  366.                 {
  367.                     searchNames.Items.Add("Choose Saved Search...");
  368.                 }
  369.                 
  370.             }
  371.             else
  372.             {
  373.                 //if file is not found, create a new xml file
  374.                 XmlTextWriter xmlWriter = new XmlTextWriter(GetWebConfigFileName(), Encoding.UTF8);
  375.                 xmlWriter.Formatting = Formatting.Indented;
  376.                 xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
  377.                 xmlWriter.WriteStartElement("SavedWebSearches");
  378.                 xmlWriter.Close();
  379.                 xmlDoc.Load(GetWebConfigFileName());
  380.                 searchNames.Items.Add("Choose Saved Search...");
  381.             }
  382.            
  383.             return searchNames;
  384.         }
  385.  
  386.         public ArrayList loadAutoSearches()
  387.         {
  388.             //Method to load saved search information into a SavedSearch object
  389.  
  390.             ArrayList searchArray = new ArrayList();
  391.             
  392.  
  393.             if (File.Exists(GetWebConfigFileName()))
  394.             {
  395.                 try
  396.                 {
  397.                     XmlDocument xmlDoc = new XmlDocument();
  398.                     xmlDoc.Load(GetWebConfigFileName());
  399.                     XmlNode node;
  400.                     XmlNodeList searchlist;
  401.                     string yes = "True";
  402.                     searchlist = xmlDoc.DocumentElement.SelectNodes("Search/SearchName[@autoShowSearch='" + yes + "']");
  403.  
  404.                     foreach (XmlNode searchName in searchlist)
  405.                     {
  406.                         SavedSearch search = new SavedSearch();
  407.                         search = loadSearch(searchName.InnerText);
  408.                         searchArray.Add(search);
  409.                     }
  410.                     searchlist = xmlDoc.DocumentElement.SelectNodes("Search/SearchName[@autoRecordSearch='" + yes + "']");
  411.  
  412.                     foreach (XmlNode searchName in searchlist)
  413.                     {
  414.                         SavedSearch search = new SavedSearch();
  415.                         search = loadSearch(searchName.InnerText);
  416.                         searchArray.Add(search);
  417.                     }
  418.                 }
  419.                 catch
  420.                 {
  421.                 }
  422.             }
  423.             searchArray.TrimToSize();
  424.             return searchArray;
  425.         }
  426.  
  427.         public SavedSearch loadSearch(string searchName)
  428.         {
  429.             //Method to load saved search information into a SavedSearch object
  430.             SavedSearch search = new SavedSearch();
  431.  
  432.             if (File.Exists(GetWebConfigFileName()))
  433.             {
  434.                 try
  435.                 {
  436.                     XmlDocument xmlDoc = new XmlDocument();
  437.                     xmlDoc.Load(GetWebConfigFileName());
  438.                     search.setSearchName(searchName);
  439.                     XmlNode node;
  440.                     XmlNodeList nodelist;
  441.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchName");
  442.                     try
  443.                     {
  444.                         search.setautoShowSearch(Convert.ToBoolean(node.Attributes["autoShowSearch"].InnerText)); 
  445.                     }
  446.                     catch
  447.                     {
  448.                         
  449.                     }
  450.                     try
  451.                     {
  452.                         search.setautoRecordSearch(Convert.ToBoolean(node.Attributes["autoRecordSearch"].InnerText));
  453.                     }
  454.                     catch
  455.                     {
  456.  
  457.                     }
  458.                     try
  459.                     {
  460.                         search.setSearchCaseSensitive(Convert.ToBoolean(node.Attributes["caseSensitiveSearch"].InnerText));
  461.                     }
  462.                     catch
  463.                     {
  464.  
  465.                     }
  466.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchVersion");
  467.                     if (node != null)
  468.                     {
  469.                         search.setSearchVersion(node.InnerText);  
  470.                     }
  471.                     else
  472.                     {
  473.                         search.setSearchVersion(null);
  474.                     }
  475.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchTitle");
  476.                     search.setSearchTitle(Convert.ToBoolean(node.InnerText));
  477.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchDescription");
  478.                     search.setSearchDescription(Convert.ToBoolean(node.InnerText));
  479.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchSubTitle");
  480.                     search.setSearchSubTitle(Convert.ToBoolean(node.InnerText));
  481.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchPhrase");
  482.                     search.setSearchPhrase(node.InnerText);
  483.                     nodelist = xmlDoc.DocumentElement.SelectNodes("Search[@id='" + searchName + "']//SearchGenre/Genre");
  484.                     foreach (XmlNode genre in nodelist)
  485.                     {
  486.                         search.addSearchGenres(genre.InnerText);
  487.                     }
  488.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchLengthMin");
  489.                     search.setSearchLengthMin(node.InnerText);
  490.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchLengthMax");
  491.                     search.setSearchLengthMax(node.InnerText);
  492.                     node = xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/TreeviewSearchOrder");
  493.                     search.setTreeviewSortOrder(node.InnerText);
  494.                     nodelist = xmlDoc.DocumentElement.SelectNodes("Search[@id='" + searchName + "']//SearchChannels/Channel");
  495.                     foreach (XmlNode channel in nodelist)
  496.                     {
  497.                         search.addSearchChannels(channel.InnerText);
  498.                     }
  499.  
  500.                     //Load the start and end dates and times if they were entered for the search.
  501.                     try
  502.                     {
  503.                         node =
  504.                             xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchStartDate");
  505.                         search.setSearchStartDate(node.InnerText);
  506.                     }
  507.                     catch(Exception x)
  508.                     {
  509.                     }
  510.                     try
  511.                     {
  512.                         node =
  513.                             xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchStartTime");
  514.                         search.setSearchStartTime(node.InnerText);
  515.                     }
  516.                     catch (Exception x)
  517.                     {
  518.                     }
  519.                     try
  520.                     {
  521.                         node =
  522.                             xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchEndDate");
  523.                         search.setSearchEndDate(node.InnerText);
  524.                     }
  525.                     catch (Exception x)
  526.                     {
  527.                     }
  528.                     try
  529.                     {
  530.                         node =
  531.                             xmlDoc.DocumentElement.SelectSingleNode("Search[@id='" + searchName + "']/SearchEndTime");
  532.                         search.setSearchEndTime(node.InnerText);
  533.                     }
  534.                     catch (Exception x)
  535.                     {
  536.                     }
  537.                 }
  538.                 catch
  539.                 {
  540.                 }
  541.             }
  542.             return search;
  543.         }
  544.  
  545.         public void deleteSearch(string searchName)
  546.         {
  547.             //Method to remove saved search from the saved search XML file
  548.             XmlDocument xmlDoc = new XmlDocument();
  549.             if (File.Exists(GetWebConfigFileName()))
  550.             {
  551.                 xmlDoc.Load(GetWebConfigFileName());
  552.                 XmlNode node = xmlDoc.SelectSingleNode("descendant::Search[@id='" + searchName + "']");
  553.                 if (node != null)
  554.                 {
  555.                     xmlDoc.DocumentElement.RemoveChild(node);
  556.                     xmlDoc.Save(GetWebConfigFileName());
  557.                 }
  558.             }
  559.         }
  560.  
  561.         public void writeSearch(SavedSearch search)
  562.         {
  563.             //Method to write a SavedSearch object to the saved search XML file
  564.             try
  565.             {
  566.                 XmlDocument xmlDoc = new XmlDocument();
  567.  
  568.                 if (File.Exists(GetWebConfigFileName()))
  569.                 {
  570.                     xmlDoc.Load(GetWebConfigFileName());
  571.                 }
  572.                 else
  573.                 {
  574.                     //if file is not found, create a new xml file
  575.                     XmlTextWriter xmlWriter = new XmlTextWriter(GetWebConfigFileName(), Encoding.UTF8);
  576.                     xmlWriter.Formatting = Formatting.Indented;
  577.                     xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
  578.                     xmlWriter.WriteStartElement("SavedWebSearches");
  579.                     xmlWriter.Close();
  580.                     xmlDoc.Load(GetWebConfigFileName());
  581.                 }
  582.                 XmlNode root = xmlDoc.DocumentElement;
  583.                 XmlElement node1 = xmlDoc.CreateElement("Search");
  584.                 node1.SetAttribute("id", search.getSearchName());
  585.                 root.AppendChild(node1);
  586.  
  587.                 XmlElement node2a = xmlDoc.CreateElement("SearchVersion");
  588.                 XmlText node2aText = xmlDoc.CreateTextNode(search.getSearchVersion());
  589.                 node1.AppendChild(node2a);
  590.                 node2a.AppendChild(node2aText);
  591.  
  592.                 XmlElement node2 = xmlDoc.CreateElement("SearchName");
  593.                 XmlText node2Text = xmlDoc.CreateTextNode(search.getSearchName());
  594.                 node2.SetAttribute("autoShowSearch", Convert.ToString(search.isAutoShowSearch()));
  595.                 node2.SetAttribute("autoRecordSearch", Convert.ToString(search.isAutoRecordSearch()));
  596.                 node2.SetAttribute("caseSensitiveSearch", Convert.ToString(search.isCaseSensitiveSearch()));
  597.                 node1.AppendChild(node2);
  598.                 node2.AppendChild(node2Text);
  599.  
  600.                 XmlElement node3 = xmlDoc.CreateElement("SearchTitle");
  601.                 XmlText node3Text = xmlDoc.CreateTextNode(search.getSearchTitle().ToString());
  602.                 node1.AppendChild(node3);
  603.                 node3.AppendChild(node3Text);
  604.  
  605.                 XmlElement node4 = xmlDoc.CreateElement("SearchSubTitle");
  606.                 XmlText node4Text = xmlDoc.CreateTextNode(search.getSearchSubTitle().ToString());
  607.                 node1.AppendChild(node4);
  608.                 node4.AppendChild(node4Text);
  609.  
  610.                 XmlElement node5 = xmlDoc.CreateElement("SearchDescription");
  611.                 XmlText node5Text = xmlDoc.CreateTextNode(search.getSearchDescription().ToString());
  612.                 node1.AppendChild(node5);
  613.                 node5.AppendChild(node5Text);
  614.  
  615.                 XmlElement node6 = xmlDoc.CreateElement("SearchPhrase");
  616.                 XmlText node6Text = xmlDoc.CreateTextNode(search.getSearchPhrase());
  617.                 node1.AppendChild(node6);
  618.                 node6.AppendChild(node6Text);
  619.  
  620.                 XmlElement node7 = xmlDoc.CreateElement("SearchGenre");
  621.                 node1.AppendChild(node7);
  622.                 foreach(string genre in search.getSearchGenres())
  623.                 {
  624.                     XmlElement node7a = xmlDoc.CreateElement("Genre");
  625.                     XmlText node7aText = xmlDoc.CreateTextNode(genre);
  626.                     node7.AppendChild(node7a);
  627.                     node7a.AppendChild(node7aText);
  628.                 }
  629.  
  630.                 XmlElement node8 = xmlDoc.CreateElement("SearchLengthMin");
  631.                 XmlText node8Text = xmlDoc.CreateTextNode(search.getSearchLengthMin());
  632.                 node1.AppendChild(node8);
  633.                 node8.AppendChild(node8Text);
  634.  
  635.                 XmlElement node9 = xmlDoc.CreateElement("SearchLengthMax");
  636.                 XmlText node9Text = xmlDoc.CreateTextNode(search.getSearchLengthMax());
  637.                 node1.AppendChild(node9);
  638.                 node9.AppendChild(node9Text);
  639.  
  640.                 XmlElement node10 = xmlDoc.CreateElement("TreeviewSearchOrder");
  641.                 XmlText node10Text = xmlDoc.CreateTextNode(search.getTreeViewSortOrder());
  642.                 node1.AppendChild(node10);
  643.                 node10.AppendChild(node10Text);
  644.  
  645.                 XmlElement node11 = xmlDoc.CreateElement("SearchChannels");
  646.                 node1.AppendChild(node11);
  647.                 foreach (string channel in search.getSearchChannels())
  648.                 {
  649.                     XmlElement node11a = xmlDoc.CreateElement("Channel");
  650.                     XmlText node11aText = xmlDoc.CreateTextNode(channel);
  651.                     node11.AppendChild(node11a);
  652.                     node11a.AppendChild(node11aText);
  653.                 }
  654.  
  655.                 //Write the search start and end dates and times
  656.                 XmlElement node12 = xmlDoc.CreateElement("SearchStartDate");
  657.                 XmlText node12Text = xmlDoc.CreateTextNode(search.getSearchStartDate());
  658.                 node1.AppendChild(node12);
  659.                 node12.AppendChild(node12Text);
  660.  
  661.                 XmlElement node13 = xmlDoc.CreateElement("SearchStartTime");
  662.                 XmlText node13Text = xmlDoc.CreateTextNode(search.getSearchStartTime());
  663.                 node1.AppendChild(node13);
  664.                 node13.AppendChild(node13Text);
  665.  
  666.                 XmlElement node14 = xmlDoc.CreateElement("SearchEndDate");
  667.                 XmlText node14Text = xmlDoc.CreateTextNode(search.getSearchEndDate());
  668.                 node1.AppendChild(node14);
  669.                 node14.AppendChild(node14Text);
  670.  
  671.                 XmlElement node15 = xmlDoc.CreateElement("SearchEndTime");
  672.                 XmlText node15Text = xmlDoc.CreateTextNode(search.getSearchEndTime());
  673.                 node1.AppendChild(node15);
  674.                 node15.AppendChild(node15Text);
  675.  
  676.                 xmlDoc.Save(GetWebConfigFileName());
  677.             }
  678.             catch (Exception ex)
  679.             {
  680.                 Logger.Error(ex.ToString());
  681.             }
  682.         }
  683.  
  684.         public void autoSearch(ArrayList Searches)
  685.         {
  686.             Settings guideParams = Global.Settings;
  687.  
  688.             Schedule scheduleHelper = Global.Schedule;
  689.             ArrayList reminders = scheduleHelper.GetReminderList();
  690.  
  691.             //Set recording settings for auto-record searches
  692.             
  693.             //Set pre/post padding
  694.             XmlNode pre = Global.Config.SelectSingleNode("/settings/PreShowPadding");
  695.             XmlNode post = Global.Config.SelectSingleNode("/settings/PostShowPadding");
  696.             int prePad = Convert.ToInt32(pre.InnerText);
  697.             int postPad = Convert.ToInt32(post.InnerText);
  698.  
  699.             //Set the recording quality
  700.             Schedule.Quality quality = 0;
  701.             switch (Global.Settings.recordingQuality.ToLower())
  702.             {
  703.                 case "high":
  704.                     quality = Schedule.Quality.High;
  705.                     break;
  706.  
  707.                 case "medium":
  708.                     quality = Schedule.Quality.Medium;
  709.                     break;
  710.  
  711.                 case "low":
  712.                     quality = Schedule.Quality.Low;
  713.                     break;
  714.  
  715.                 case "custom1":
  716.                     quality = Schedule.Quality.Custom1;
  717.                     break;
  718.  
  719.                 case "custom2":
  720.                     quality = Schedule.Quality.Custom2;
  721.                     break;
  722.             }
  723.  
  724.             // Search for an episode listing, set the start time and the end time
  725.             DateTime startTime = DateTime.Now;
  726.             DateTime endTime = startTime.AddDays(guideParams.autoSearchDays);
  727.             IList allChannels = scheduleHelper.GetListingsForTimePeriod(startTime, endTime);
  728.  
  729.             CaseInsensitiveComparer caseInsensitiveComparer = new CaseInsensitiveComparer();
  730.  
  731.             int x = 0;
  732.  
  733.             foreach (SavedSearch search in Searches)
  734.             {
  735.                 x++;
  736.                 
  737.                 int minLength = int.MinValue;
  738.                 int maxLength = int.MaxValue;
  739.                 bool checkTitle = search.getSearchTitle();
  740.                 bool checkDesc = search.getSearchDescription();
  741.                 bool checkSubtitle = search.getSearchSubTitle();
  742.                 string searchFor = search.getSearchPhrase();
  743.                 ArrayList channels = search.getSearchChannels();
  744.                 ArrayList selectedGenre = search.getSearchGenres();
  745.  
  746.                 if ((search.getSearchLengthMin() != null) && (search.getSearchLengthMin().Length > 0)) minLength = int.Parse(search.getSearchLengthMin());
  747.                 if ((search.getSearchLengthMax() != null) && (search.getSearchLengthMax().Length > 0)) maxLength = int.Parse(search.getSearchLengthMax());
  748.  
  749.                 DateTime savedStartDate = DateTime.Now;
  750.                 DateTime savedStartTime = DateTime.Now;
  751.                 DateTime savedEndDate = DateTime.Now;
  752.                 DateTime savedEndTime = DateTime.Now;
  753.                 int parmStartDateSum = 0;
  754.                 int parmEndDateSum = 0;
  755.                 bool defaultStartDateTime = false;
  756.                 bool defaultEndDateTime = false;
  757.                 bool validStartDate = false;
  758.                 bool validStartTime = false;
  759.                 bool validEndDate = false;
  760.                 bool validEndTime = false;
  761.  
  762.                 SavedSearchUtils searchutil = new SavedSearchUtils();
  763.  
  764.                 //To clean up the mess I made with storing funky dates when I added a time and date parm we need to check to see if the date and time
  765.                 //as a pair are a default value (meaning no date and time was entered) as well as individual defaults.  To correct this moving forward I 
  766.                 //added a search version and can now update the search with correct default values as needed by simply resaving the search after corrections
  767.                 //or updates are applied here.
  768.                 //
  769.                 //1/1/0001 and 12:00 a.m. = default date & time that needs to be removed and set to nulls
  770.                 //
  771.                 //Search version checking is done here in auto-search as well as in the search and search2 pages.
  772.                 if (search.getSearchStartDate() == "1/1/0001" && search.getSearchStartTime() == "12:00 AM")
  773.                 {
  774.                     defaultStartDateTime = true;
  775.                     search.setSearchStartDate(null);
  776.                     search.setSearchStartTime(null);
  777.                 }
  778.  
  779.                 if (search.getSearchStartDate() == "1/1/0001")
  780.                 {
  781.                     search.setSearchStartDate(null);
  782.                 }
  783.  
  784.                 if (search.getSearchEndDate() == "1/1/0001" && search.getSearchEndTime() == "12:00 AM")
  785.                 {
  786.                     defaultEndDateTime = true;
  787.                     search.setSearchEndDate(null);
  788.                     search.setSearchEndTime(null);
  789.                 }
  790.  
  791.                 if (search.getSearchEndDate() == "1/1/0001")
  792.                 {
  793.                     search.setSearchEndDate(null);
  794.                 }
  795.  
  796.                 if (search.getSearchStartDate() != null && search.getSearchStartDate() != "" && search.getSearchStartDate() != "1/1/0001")
  797.                 {
  798.                     savedStartDate = searchutil.toDate(search.getSearchStartDate());
  799.                     parmStartDateSum = savedStartDate.Month + savedStartDate.Day + savedStartDate.Year;
  800.                     validStartDate = true;
  801.                 }
  802.  
  803.                 if (search.getSearchStartTime() != null && search.getSearchStartTime() != "" && !defaultStartDateTime)
  804.                 {
  805.                     savedStartTime = searchutil.toDate(search.getSearchStartTime());
  806.                     validStartTime = true;
  807.                 }
  808.  
  809.                 if (search.getSearchEndDate() != null && search.getSearchEndDate() != "" && search.getSearchEndDate() != "1/1/0001")
  810.                 {
  811.                     savedEndDate = searchutil.toDate(search.getSearchEndDate());
  812.                     parmEndDateSum = savedEndDate.Month + savedEndDate.Day + savedEndDate.Year;
  813.                     validEndDate = true;
  814.                 }
  815.  
  816.                 if (search.getSearchEndTime() != null && search.getSearchEndTime() != "" && !defaultEndDateTime)
  817.                 {
  818.                     savedEndTime = searchutil.toDate(search.getSearchEndTime());
  819.                     validEndTime = true;
  820.                 }
  821.  
  822.                 if (search.getSearchVersion() != guideParams.CurrentSearchVersion)
  823.                 {
  824.                     search.setSearchVersion(guideParams.CurrentSearchVersion);
  825.                     string name = search.getSearchName();
  826.                     searchutil.deleteSearch(name);
  827.                     searchutil.writeSearch(search);
  828.                 }
  829.                 
  830.                 foreach (Channel channel in allChannels)
  831.                 {
  832.                     foreach (Programme program in channel.getProgrammeList())
  833.                     {
  834.                         if (searchFor != null)
  835.                         {
  836.                             if (!search.isCaseSensitiveSearch())
  837.                             {
  838.                                 searchFor = searchFor.ToUpper();
  839.                             }
  840.                         }
  841.                         bool showProgram = !checkTitle && !checkDesc && !checkSubtitle;
  842.  
  843.                         if (!search.isCaseSensitiveSearch())
  844.                         {
  845.                             if (checkTitle) showProgram |= program.getTitle().ToUpper().IndexOf(searchFor) > -1;
  846.                             if (checkDesc) showProgram |= program.getDescription().ToUpper().IndexOf(searchFor) > -1;
  847.                             if (checkSubtitle) showProgram |= program.getSubTitle().ToUpper().IndexOf(searchFor) > -1;
  848.                         }
  849.                         else
  850.                         {
  851.                             if (checkTitle) showProgram |= program.getTitle().IndexOf(searchFor) > -1;
  852.                             if (checkDesc) showProgram |= program.getDescription().IndexOf(searchFor) > -1;
  853.                             if (checkSubtitle) showProgram |= program.getSubTitle().IndexOf(searchFor) > -1;
  854.                         }
  855.  
  856.                         double length = (program.getEndTime() - program.getStartTime()).TotalMinutes;
  857.                         showProgram &= (length >= minLength) && (length <= maxLength);
  858.  
  859.                         int pgmStartDateSum = program.getStartTime().Month + program.getStartTime().Day + program.getStartTime().Year;
  860.                         int pgmEndDateSum = program.getEndTime().Month + program.getEndTime().Day + program.getEndTime().Year;
  861.  
  862.  
  863.                         //Date processing logic to compare programmes that have already matched a entered search parameter
  864.                         //StartDate, EndDate, StartTime and EndTime will be null if they were not entered  
  865.                         if (showProgram)
  866.                         {
  867.                             //Check to see if the passed in start date is a dummy date
  868.                             if (showProgram && validStartDate)
  869.                             {
  870.                                 if (pgmStartDateSum < parmStartDateSum)
  871.                                 {
  872.                                     showProgram = false;
  873.                                 }
  874.                             }
  875.  
  876.                             //Check to see if the passed in start time is a dummy time
  877.                             if (showProgram && validStartTime)
  878.                             {
  879.                                 int Cmpr =
  880.                                     String.Compare(program.getStartTime().ToString("HH:mm"), savedStartTime.ToString("HH:mm"));
  881.  
  882.                                 if (Cmpr < 0)
  883.                                 {
  884.                                     showProgram = false;
  885.                                 }
  886.                             }
  887.  
  888.                             //Check to see if the passed in end date is a dummy date
  889.                             if (showProgram && validEndDate)
  890.                             {
  891.                                 if (pgmEndDateSum > parmEndDateSum)
  892.                                 {
  893.                                     showProgram = false;
  894.                                 }
  895.                             }
  896.  
  897.                             //Check to see if the passed in end time is a dummy time
  898.                             if (showProgram && validEndTime)
  899.                             {
  900.                                 int Cmpr =
  901.                                     String.Compare(program.getEndTime().ToString("HH:mm"), savedEndTime.ToString("HH:mm"));
  902.  
  903.                                 if (Cmpr > 0)
  904.                                 {
  905.                                     showProgram = false;
  906.                                 }
  907.                             }
  908.                         }
  909.  
  910.                         if (showProgram && (selectedGenre != null) && (selectedGenre.Count > 0))
  911.                         {
  912.                             if (program.getGenreList().Count > 0)
  913.                             {
  914.                                 ArrayList showGenres = new ArrayList(program.getGenreList());
  915.                                 showGenres.Sort();
  916.                                 foreach (string genre in selectedGenre)
  917.                                 {
  918.                                     int idx = showGenres.BinarySearch(genre, caseInsensitiveComparer);
  919.                                     if (idx > -1)
  920.                                     {
  921.                                         showProgram = true;
  922.                                         break;
  923.                                     }
  924.                                     showProgram = false;
  925.                                 }
  926.                             }
  927.                             else
  928.                             {
  929.                                 showProgram = false;
  930.                             }
  931.                         }
  932.  
  933.                         if (channels.Count > 0)
  934.                             showProgram &= channels.Contains(channel.channelNumber.ToString());
  935.  
  936.                         if (showProgram)
  937.                         {
  938.                             if (guideParams.autoShowRecord && search.isAutoRecordSearch())
  939.                             {
  940.                                 IDictionary knownRecordings = scheduleHelper.LoadKnownRecordings();
  941.                                 if (!knownRecordings.Contains(program.getOID()))
  942.                                 {
  943.                                     Schedule myschedule = Global.Schedule;
  944.                                     bool scheduleReturn = myschedule.ScheduleOnce(program, quality, prePad, postPad);
  945.                                     scheduleHelper.RemoveReminder(program);
  946.                                 }
  947.                             }
  948.                             else
  949.                             {
  950.                                 if (guideParams.autoShowSearch && search.isAutoShowSearch())
  951.                                 {
  952.                                     if (!reminders.Contains(program))
  953.                                     {
  954.                                         scheduleHelper.CreateReminder(program);
  955.                                     }
  956.                                 }
  957.                             } 
  958.                         }
  959.                     }
  960.                 }
  961.             }
  962.         }
  963.  
  964.         private string webConfigFileName;
  965.  
  966.         private string GetWebConfigFileName()
  967.         {
  968.             if (webConfigFileName == null)
  969.             {
  970.                 webConfigFileName = Path.Combine(GetInstallDir(), "web-saved-searches.xml");
  971.             }
  972.             return webConfigFileName;
  973.         }
  974.  
  975.         public string GetInstallDir()
  976.         {
  977.             // determine install location
  978.             string installDirectory = @"c:\program files\devnz\gbpvr\";
  979.             try
  980.             {
  981.                 using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\devnz\"))
  982.                 {
  983.                     string temp = (string)key.GetValue("GBPVR InstallDir");
  984.                     if (temp != null)
  985.                     {
  986.                         installDirectory = temp;
  987.                     }
  988.                 }
  989.             }
  990.             catch (Exception ex)
  991.             {
  992.                 Logger.Error("Error reading install location from registry: " + ex.ToString());
  993.             }
  994.             return installDirectory;
  995.         }
  996.     }
  997.     #endregion
  998. }